﻿''' <summary>
''' Helper class represents a single entry in a MenuScreen. By default this
''' just draws the entry text string, but it can be customized to display menu
''' entries in different ways. This also provides an event that will be raised
''' when the menu entry is selected.
''' </summary>
Class MenuEntry
#Region "Fields"

	

	''' <summary>
	''' Tracks a fading selection effect on the entry.
	''' </summary>
	''' <remarks>
	''' The entries transition out of the selection effect when they are deselected.
	''' </remarks>
	Dim _selectionFade!


#End Region

#Region "Properties"


	''' <summary>
	''' Gets or sets the text of this menu entry.
	''' </summary>
	Public Property Text As String


	''' <summary>
	''' Gets or sets the position at which to draw this menu entry.
	''' </summary>
	Public Property Position As Vector2


#End Region

#Region "Events"


	''' <summary>
	''' Event raised when the menu entry is selected.
	''' </summary>
	Public Event Selected As EventHandler(Of PlayerIndexEventArgs)


	''' <summary>
	''' Method for raising the Selected event.
	''' </summary>
	Protected Friend Overridable Sub OnSelectEntry(Index As PlayerIndex)
		If Not (Selected Is Nothing) Then _
   Selected(Me, New PlayerIndexEventArgs(Index))
	End Sub


#End Region

#Region "Initialization"


	''' <summary>
	''' Constructs a new menu entry with the specified text.
	''' </summary>
	Public Sub New(text$)
		Me._Text = text
	End Sub


#End Region

#Region "Update and Draw"


	''' <summary>
	''' Updates the menu entry.
	''' </summary>
	Public Overridable Sub Update(screen As MenuScreen, isSelected As Boolean, Time As GameTime)
		' there is no such thing as a selected item on Windows Phone, so we always
		' force isSelected to be false
#If WINDOWS_PHONE Then
		isSelected = False
#End If

		' When the menu selection changes, entries gradually fade between
		' their selected and deselected appearance, rather than instantly
		' popping to the new state.
		Dim fadeSpeed! = CSng(Time.ElapsedGameTime.TotalSeconds * 4)

		If (isSelected) Then
			_selectionFade = Math.Min(_selectionFade + fadeSpeed, 1)
		Else
			_selectionFade = Math.Max(_selectionFade - fadeSpeed, 0)
		End If
	End Sub

		''' <summary>
		''' Draws the menu entry. This can be overridden to customize the appearance.
		''' </summary>
	Public Overridable Sub Draw(screen As MenuScreen, isSelected As Boolean, Time As GameTime)
		' there is no such thing as a selected item on Windows Phone, so we always
		' force isSelected to be false
#If WINDOWS_PHONE Then
		isSelected = False
#End If

		' Draw the selected entry in yellow, otherwise white.
		Dim _color As Color
		If isSelected Then _color = Color.Yellow Else _color = Color.White

		' Pulsate the size of the selected menu entry.
		Dim _time# = Time.TotalGameTime.TotalSeconds

		Dim pulsate! = CSng(Math.Sin(_time * 6) + 1)

		Dim scale! = 1 + pulsate * 0.05F * _selectionFade

		' Modify the alpha to fade text out during transitions.
		_color *= screen.TransitionAlpha

		' Draw text, centered on the middle of each line.
		Dim Manager As ScreenManager = screen.ScreenManager
		Dim _spriteBatch As SpriteBatch = Manager.SpriteBatch
		Dim font As SpriteFont = Manager.Font

		Dim origin As Vector2 = New Vector2(0, CSng(font.LineSpacing / 2))

		'spriteBatch.DrawString(font, text, position + new Vector2(4, 4), Color.Black, 0,
		'    origin, scale, SpriteEffects.None, 0);
		'spriteBatch.DrawString(font, text, position, color, 0,
		'                       origin, scale, SpriteEffects.None, 0);

		_spriteBatch.DrawString(font, _Text, _Position, _color, 0,
							   origin, scale, SpriteEffects.None, 0)
	End Sub


		''' <summary>
		''' Queries how much space this menu entry requires.
		''' </summary>
	Public Overridable Function GetHeight%(screen As MenuScreen)
		Return screen.ScreenManager.Font.LineSpacing
	End Function


	''' <summary>
	''' Queries how wide the entry is, used for centering on the screen.
	''' </summary>
	Public Overridable Function GetWidth%(screen As MenuScreen)
		Return CInt(screen.ScreenManager.Font.MeasureString(Text).X)
	End Function


#End Region
End Class